﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Ocmf.Forms;

namespace Ocmf.Types
{
    public class GoTo:Action
    {
        private bool isConditional;
        public bool IsConditional
        {
            get
            {
                return isConditional;
            }
            set
            {
                isConditional = value;
                if (isConditional)
                    Text = " If " + Counter + " is " + Enum.GetName(typeof (Types.Inequality), Inequality) + " " + ConditionValue + " go to trial " + TrialIndex;
                else
                    Text = "Go To Trial " + TrialIndex;
            }
        }
        
        private int trialIndex;
        public int TrialIndex
        {
            get
            {
                return trialIndex;
            }
            set
            {
                trialIndex = value;
                if (isConditional)
                    Text = " If " + Counter + " is " + Enum.GetName(typeof(Types.Inequality), Inequality) + " " + ConditionValue + " go to trial " + TrialIndex;
                else
                    Text = "Go To Trial " + TrialIndex;
            }
        }

        private string counter;
        public string Counter
        {
            get
            {
                return counter;
            }
            set
            {
                counter = value;
                if (isConditional)
                    Text = " If " + Counter + " is " + Enum.GetName(typeof(Types.Inequality), Inequality) + " " + ConditionValue + " go to trial " + TrialIndex;
                else
                    Text = "Go To Trial " + TrialIndex;
            }
        }

        private Inequality inequality;
        public Inequality Inequality
        {
            get
            {
                return inequality;
            }
            set
            {
                inequality = value;
                if (isConditional)
                    Text = " If " + Counter + " is " + Enum.GetName(typeof(Types.Inequality), Inequality) + " " + ConditionValue + " go to trial " + TrialIndex;
                else
                    Text = "Go To Trial " + TrialIndex;
            }
        }

        private int conditionValue;
        public int ConditionValue
        {
            get
            {
                return conditionValue;
            }
            set
            {
                conditionValue = value;
                if (isConditional)
                    Text = " If " + Counter + " is " + Enum.GetName(typeof(Types.Inequality), Inequality) + " " + ConditionValue + " go to trial " + TrialIndex;
                else
                    Text = "Go To Trial " + TrialIndex;
            }
        }

        public GoTo()
        {
            Text = "Go To Next Trial";
            SelectedImageKey = ImageKey = "goto";
            //set default goto index
            //set default image
            //create container for goto rules
        }

        public override object Clone()
        {
            var clone = base.Clone() as GoTo;
            clone.TrialIndex = TrialIndex;
            return clone;
        }

        public override Form Editor
        {
            get { return new EditGoToActionForm(this); }
        }

        public override XmlElement ToXml(XmlDocument xmlDocument)
        {
            //create trial xml element
            var gotoElement = xmlDocument.CreateElement("GoTo");
            //create, set and add goto type
            var gotoType = xmlDocument.CreateAttribute("IsConditional");
            gotoType.InnerText = isConditional.ToString();
            gotoElement.Attributes.Append(gotoType);
            if (isConditional)
            {
                var counterElement = xmlDocument.CreateElement("Counter");
                counterElement.InnerText = counter;
                gotoElement.AppendChild(counterElement);

                var inequalityElement = xmlDocument.CreateElement("Inequality");
                inequalityElement.InnerText = Enum.GetName(typeof (Types.Inequality), Inequality);
                gotoElement.AppendChild(inequalityElement);

                var conditonalValueElement = xmlDocument.CreateElement("ConditionalValue");
                conditonalValueElement.InnerText = conditionValue.ToString();
                gotoElement.AppendChild(conditonalValueElement);
            }

            //create, set and add destination trial index
            var destinationTrialIndexNode = xmlDocument.CreateElement("DestinationTrialIndex");
            destinationTrialIndexNode.InnerText = trialIndex.ToString();
            gotoElement.AppendChild(destinationTrialIndexNode);
            //return element
            return gotoElement;
        }

        public override void FromXml(XmlNode xmlElement)
        {
            IsConditional = bool.Parse(xmlElement.Attributes["IsConditional"].InnerText);
            
            if (IsConditional)
            {
                Counter = xmlElement["Counter"].InnerText;
                Inequality = (Inequality) Enum.Parse(typeof (Types.Inequality), xmlElement["Inequality"].InnerText);
                ConditionValue = Int32.Parse(xmlElement["ConditionalValue"].InnerText);
            }
            
            TrialIndex = Int32.Parse(xmlElement["DestinationTrialIndex"].InnerText);
        }
    }
}
